<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use think\facade\App;

/**
 * 文件api统一入口
 */
class File extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 打包下载文件
	 */
	public function packDownload(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['reportId'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				
		        $w['isDelete'] = 'N';
		        $w['reportId'] = $in['reportId'];
		        $list = controller('service/ReportEvd') -> getMuchData($w);
				if($list['code'] != '1') returnResults(['code'=>-1, 'msg'=>'操作失败,该案件没有结案文件']);
				$list = $list['info'];
				
				$reportIn[] = ['id', 'eq', $in['reportId'][0]];
				$report = controller("service/Report") -> getSingleData($reportIn);
				if($report['code'] != '1') returnResults($report);
//				
				$time = mb_substr(md5(time()),0,6);
		        $zipname = './exportsup/'.$report['info']['code'].'.zip';
				$filePath = './uploads/' . date("Ymd", time());
		        $files = [];
		        foreach($list as $v){
		            $tmp = [];
		            $tmp['name'] = $v['name'];
					if( strpos($v['url'], 'https://') !== false ){
						$fileStr = file_get_contents($v['url']);
						if(!is_dir($filePath)){
							$mkdirRe = mkdir($filePath, 0777, TRUE);
							if(!$mkdirRe) returnResults(['code'=>'-1', 'msg'=>'文件夹创建失败']);
						}
						$url = $filePath . '/' . md5($v['url']) . rand(1000, 9999);
						file_put_contents($url, $fileStr);
						$tmp['url'] = $url;
					}else{
			            $tmp['url'] = '.' . $v['url'];
					}
		            $files[] = $tmp;
		        }

		        $zip = new \ZipArchive();
		
		        $zipname = '/exportsup/'.$report['info']['code'].'.zip';
				
				$zip->open('.' . $zipname, \ZipArchive::CREATE);
				
		        foreach ($files as $k=>$file) {
		            $r = $zip->addFile("./".$file['url'], '/'.$file['name']);
		        }
		
		
		        $zip->close();
		
				$outArr['code'] = '1';
				$outArr['info']['code'] = $report['info']['code'];
				$outArr['info']['file'] = $zipname;
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 上传文件
	 */
	public function uploadFile(){
		if(request() -> isPost()){
			$in = input("post.");
			$inFile = $_FILES['file'];
			$bool = is_empty([], $in);
			if($bool || empty($inFile)){
				$outArr['code'] = '40004';
			}else{
				
				
				$suffix = explode('/', $inFile['type']);
				$suffix = substr(strrchr($inFile['name'], '.'), 1);
				
				$fileConfig = config("QI_NIU");
				
//				校验文件格式
				if( !in_array($suffix, $fileConfig['suffix']['type']) ){
					returnResults(['code'=>'40005','info'=>$suffix]);
				}
				
//				校验文件大小
				if( !$inFile['size'] || empty($inFile['size']) || $inFile['size'] > $fileConfig['maxSize'] ){
					returnResults(['code'=>'40007']);
				}
				
				switch ($suffix) {
					case 'plain': $suffix = 'txt'; break;
					case 'vnd.openxmlformats-officedocument.spreadsheetml.sheet': $suffix = 'xlsx'; break;
					case 'vnd.ms-excel': $suffix = 'xls'; break;
					case 'vnd.openxmlformats-officedocument.wordprocessingml.document': $suffix = 'docx'; break;
					case 'msword': $suffix = 'doc'; break;
					case 'mpeg': $suffix = 'mp3'; break;
				}
				
				$re = controller("service/Qiniu") -> upLoadFile($inFile['tmp_name'], $suffix);
				if( $re['code'] != 1 ){
					returnResults($re);
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $fileConfig['domain'] . $re['info'];
				$outArr['param'] = $inFile;
				$outArr['suffix'] = $suffix;
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>